/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Jorley
 */
public class Conexao {
    public static Connection getConnection() throws LojaDaoException {
        try {
            System.out.println("-------- PostgreSQL "
				+ "JDBC Connection Testing ------------");
 
		try {
 
			Class.forName("org.postgresql.Driver");
 
		} catch (ClassNotFoundException e) {
 
			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
			
 
		}
//            Class.forName("org.postgresql.Driver");
            String bd = "jdbc:postgresql://localhost:5432/db_loja_virtual";
            String usr = "postgres";
            String pswrd = "admin";
            return DriverManager.getConnection(bd, usr, pswrd);
        } catch (Exception e) {
            throw new LojaDaoException(e.getMessage());
        }
    }

    public static void closeConnection(Connection conn, Statement stat,
            ResultSet rs) throws LojaDaoException {
        close(conn, stat, rs);
    }

    public static void closeConnection(Connection conn, Statement stat) 
            throws LojaDaoException {
        close(conn, stat, null);
    }

    public static void closeConnection(Connection conn) 
            throws LojaDaoException {
        close(conn, null, null);
    }
    
    private static void close(Connection conn, Statement stat, ResultSet rs) 
            throws LojaDaoException{
        try{
            if(conn != null) conn.close();
            if(stat != null) stat.close();
            if(rs != null) rs.close();
        } catch (Exception e){
            throw new LojaDaoException(e.getMessage());
        }
    }
    
}
